﻿using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;

namespace HiauPdm.Web
{
    public partial class Login : Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Response.Expires = -1;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ((TextBox)UserLogin.FindControl("Password")).Attributes["onkeydown"] = "if(event.keyCode==13){document.getElementById('" + UserLogin.FindControl("LoginButton").ClientID + "').click();}";
                ((ImageButton)UserLogin.FindControl("LoginButton")).OnClientClick = "this.disabled=true;document.getElementById('" + LblFailure.ClientID + "').innerHTML='提示：正在登录，请稍候……';" + Page.ClientScript.GetPostBackEventReference(UserLogin.FindControl("LoginButton"), null) + ";return false;";
                LblCopyright.Text = BGlobal.SystemCopyrightC + "<br />" + BGlobal.SystemCopyrightE;
                UserLogin.Focus();
            }
        }

        protected void UserLogin_Authenticate(object sender, AuthenticateEventArgs e)
        {
            int sUserId; string sRealName;
            string sErr = BMain.Login(UserLogin.UserName.Trim(), UserLogin.Password, out sUserId, out sRealName);
            if (sErr != null)
            {
                LblFailure.Text = string.Format("提示：{0}", sErr);
                UserLogin.Focus();
            }
            else
            {
                LblFailure.Text = string.Empty;
                if (Session["LoginTime"] == null)
                {
                    Application["SessionUsers"] = ((int)Application["SessionUsers"]) + 1;
                }
                Session["LoginTime"] = DateTime.Now;
                Session["UserId"] = sUserId;
                Session["RealName"] = sRealName;
                e.Authenticated = true;
            }
        }
    }
}